﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using Medianamik.Core;
using Medianamik.Core.Sugar;
using Medianamik.Globalization;

namespace Medianamik.UI.TypeManagement.Controls
{
    public partial class ManyToManyPropertySettings : UserControl
    {
        #region Properties

        public String PropertyName
        {
            get
            {
                return txtPropertyName.Text;
            }
        }

        public String GroupName
        {
            get
            {
                return txtGroupName.Text;
            }
        }


        public Guid? PrimaryPropertyID
        {
            get
            {
                var g = default(Guid);
                if (StringHelper.IsGuid(ddlPrimaryProperty.SelectedValue, out g))
                {
                    return g;
                }
                return default(Guid?);
            }
        }


        public Guid? PrimaryTypeID
        {
            get
            {
                var g = default(Guid);
                if (StringHelper.IsGuid(ddlPrimaryType.SelectedValue, out g))
                {
                    return g;
                }
                return default(Guid?);
            }
        }

        public Guid? PrimaryMedianamikControlID
        {
            get
            {
                var g = default(Guid);
                if (StringHelper.IsGuid(ddlPrimaryMedianamikControl.SelectedValue, out g))
                {
                    return g;
                }
                return default(Guid?);
            }
        }
        public int? PrimaryMedianamikControlWidth
        {
            get
            {
                int v;
                if (PrimaryMedianamikControlID.HasValue
                    && int.TryParse(txtPrimaryMedianamikControlWidth.Text, out v))
                {
                    return v;
                }
                return default(int?);
            }
        }
        public int? PrimaryMedianamikControlHeigth
        {
            get
            {
                int v;
                if (PrimaryMedianamikControlID.HasValue
                    && int.TryParse(txtPrimaryMedianamikControlHeight.Text, out v))
                {
                    return v;
                }
                return default(int?);
            }
        }
        public Guid? PrimaryTreeRootNodeId
        {
            get
            {
                var g = default(Guid);
                if (StringHelper.IsGuid(PrimaryRootNodeIdComboBox.SelectedValue, out g))
                {
                    return g;
                }
                return default(Guid?);
            }
        }

        public bool IsEditing { get; set; }

        public IDictionary<string, string> Translations
        {
            get
            {
                return PropNameTranslations.Values;
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                EnsureLoadLookupTypes();
                EnsureLoadLookupMedianamikControls();
                UpdateControlStatus();
            }
        }
        protected void Page_Init(object sender, EventArgs e)
        {
           
        }

        public void SetValues(NodeTypeProperty pProperty)
        {
            if (pProperty == null)
            {
                throw new ArgumentNullException("pProperty");
            }
            
            //PropertyName
            txtPropertyName.Text = pProperty.Name;
            PropNameTranslations.Values = Translator.GetResources("TypeManagement",
                pProperty.ID.ToString() + "_Label",
                ConfigManager.GetMedianamikCultures().Select(c => c.CultureInfo));

            txtGroupName.Text = pProperty.GroupName;

            //Selection du NodeType
            EnsureLoadLookupTypes();
            var selectedNodeType = ddlPrimaryType.Items.FindByValue(pProperty.PrimaryTypeID.ToString());
            if (selectedNodeType == null)
            {
                ddlPrimaryProperty.Items.Clear();
                return;
            }

            selectedNodeType.Selected = true;
            ddlPrimaryType_SelectedIndexChanged(this, EventArgs.Empty);

            //Selection du NodeTypeProperty
            var selectedNodeTypeProperty = ddlPrimaryProperty.Items.FindByValue(pProperty.PrimaryPropertyID.ToString());
            if (selectedNodeTypeProperty != null)
            {
                selectedNodeTypeProperty.Selected = true;
            }

            //Selection du MedianamikControl
            EnsureLoadLookupMedianamikControls();
            ddlPrimaryMedianamikControl.SelectedIndex = 0;
            if (pProperty.PrimaryMedianamikControlID.HasValue)
            {
                var selectedMedianamikControl = ddlPrimaryMedianamikControl.Items.FindByValue(pProperty.PrimaryMedianamikControlID.ToString());
                if (selectedMedianamikControl != null) 
                {
                    ddlPrimaryMedianamikControl.ClearSelection();
                    selectedMedianamikControl.Selected = true; 
                }
            }
            ddlPrimaryMedianamikControl_SelectedIndexChanged(this, EventArgs.Empty);
            
            //Selection du TreeRootNodeId
            if (pProperty.PrimaryMedianamikControlInfo.TreeRootNodeID.HasValue)
            {
                PrimaryRootNodeIdComboBox.SetSelectedNode(pProperty.PrimaryMedianamikControlInfo.TreeRootNodeID.Value);
            }
        }
        
        protected void UpdateControlStatus()
        {
            txtPropertyName.Enabled = !IsEditing;
            ddlPrimaryType.Enabled = !IsEditing;
            ddlPrimaryProperty.Enabled = !IsEditing;
        }

        protected void ddlPrimaryMedianamikControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool isMedianamikControlSelected = (ddlPrimaryMedianamikControl.SelectedIndex > 0);

            phrPrimaryMedianamikControlFields.Visible = isMedianamikControlSelected;
            if (isMedianamikControlSelected)
            {
                var mkControl = ConfigManager.GetMedianamikControl(new Guid(ddlPrimaryMedianamikControl.SelectedValue));
                phrPrimaryMedianamikControlWidth.Visible = mkControl.MaySpecifyWidth;
                if (mkControl.MaySpecifyWidth)
                {
                    extPrimaryMedianamikControlWidth.DefaultValueText = mkControl.DefaultWidth.ToString();
                }

                phrPrimaryMedianamikControlHeight.Visible = mkControl.MaySpecifyHeight;

                if (mkControl.MaySpecifyHeight)
                {
                    extPrimaryMedianamikControlHeight.DefaultValueText = mkControl.DefaultHeight.ToString();
                }
            }
        }

        protected void ddlPrimaryType_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool isTypeSelected = (ddlPrimaryType.SelectedIndex > 0);
            phrPrimaryProperty.Visible = isTypeSelected;

            if (isTypeSelected)
            {
                LoadLookupProperties(NodeTypeManager.GetNodeType(new Guid(ddlPrimaryType.SelectedValue )));
            }
        }
        private void EnsureLoadLookupTypes()
        {
            if (ddlPrimaryType.Items.Count == 0)
            {
                ddlPrimaryType.DataSource = NodeTypeManager.EligibleLookupTypes;
                ddlPrimaryType.DataBind();
                ddlPrimaryType.Items.Insert(0, "");
            }
        }

        private void EnsureLoadLookupMedianamikControls()
        {
            if (ddlPrimaryMedianamikControl.Items.Count == 0)
            {
                ddlPrimaryMedianamikControl.DataSource = ConfigManager.GetMedianamikControls(DataTypes.UniqueIdentifier, MedianamikControlType.MANY_TO_MANY);
                ddlPrimaryMedianamikControl.DataBind();
                ddlPrimaryMedianamikControl.Items.Insert(0, "");
            }
        }

        private void LoadLookupProperties(NodeType pType)
        {
            ddlPrimaryProperty.DataSource = pType.GetAllProperties().Where(p => p.DataType.ID == DataTypes.Varchar);
            ddlPrimaryProperty.DataBind();
            ddlPrimaryProperty.Items.Insert(0, "");
        }

       
    }
}